﻿using System.Collections.Generic;
using System.Windows.Forms;
using ReportWizard.DataAccess;

namespace ReportWizard.Comparers
{
    class HistoryRecordComparer : IComparer<HistoryRecord>
    {
        private HistoryRecord.SortingFields MemberName { get; set; }
        private SortOrder SortingOrder { get; set; }

        public HistoryRecordComparer()
        {
            MemberName = HistoryRecord.SortingFields.DateCreated;
            SortingOrder = SortOrder.None;
        }

        public HistoryRecordComparer(HistoryRecord.SortingFields memberName, SortOrder sortingOrder)
        {
            MemberName = memberName;
            SortingOrder = sortingOrder;
        }

        public int Compare(HistoryRecord x, HistoryRecord y)
        {
            int compareValue;

            switch (MemberName)
            {
                case HistoryRecord.SortingFields.Login:
                    {
                        compareValue = SortingOrder == SortOrder.Ascending ? x.Login.CompareTo(y.Login) : y.Login.CompareTo(x.Login);
                        break;
                    }
                case HistoryRecord.SortingFields.Action:
                    {
                        compareValue = SortingOrder == SortOrder.Ascending ? x.Action.CompareTo(y.Action) : y.Action.CompareTo(x.Action);
                        break;
                    }
                case HistoryRecord.SortingFields.Description:
                    {
                        compareValue = SortingOrder == SortOrder.Ascending ? x.Description.CompareTo(y.Description) : y.Description.CompareTo(x.Description);
                        break;
                    }
                case HistoryRecord.SortingFields.DateCreated:
                    {
                        compareValue = SortingOrder == SortOrder.Ascending ? x.DateCreated.CompareTo(y.DateCreated) : y.DateCreated.CompareTo(x.DateCreated);
                        break;
                    }
                default:
                    {
                        compareValue = SortingOrder == SortOrder.Ascending ? x.DateCreated.CompareTo(y.DateCreated) : y.DateCreated.CompareTo(x.DateCreated);
                        break;
                    }
            }

            if (compareValue == 0)
            {
                return SortingOrder == SortOrder.Ascending
                                   ? x.DateCreated.CompareTo(y.DateCreated)
                                   : y.DateCreated.CompareTo(x.DateCreated);
            }
            return compareValue;
        }
    }
}
